/**
 * User: 夏汛
 * Date: 12-5-14
 * Time: 下午5:37
 */
Ext.extend(app.module,{
    currentFormValues: {},
    init: function(){
        this.ds = new Ext.data.Store({
            proxy: new Ext.data.HttpProxy({url:'fruittype/list.do'}),
            reader: new Ext.data.JsonReader({
                totalProperty: 'total',
                root: 'rows',
                idProperty: 'id'
            },[
                {name:'id'},
                {name:'name'},
                {name:'description'}
            ])
        });
        this.sm = new Ext.grid.CheckboxSelectionModel();
        this.body = new Ext.grid.GridPanel({
            store: this.ds,
            region: 'center',
            loadMask: true,
            viewConfig:{
                forceFit:true
            },
            sm: this.sm,
            columns: [
                new Ext.grid.RowNumberer(),
                this.sm,
                {
                    xtype: 'numbercolumn',
                    dataIndex: 'id',
                    header: 'id',
                    hidden: true
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'name',
                    header: '名称'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'description',
                    header: '描述'
                }
            ],
            tbar: {
                xtype: 'toolbar',
                items: [
                    {
                        text: '新增',
                        iconCls : 'add',
                        scope: this,
                        handler: this.add
                    },
                    {
                        xtype: 'tbseparator'
                    },
                    {
                        text: '修改',
                        iconCls : 'edit',
                        scope: this,
                        handler: this.edit
                    },
                    {
                        xtype: 'tbseparator'
                    },
                    {
                        text: '删除',
                        iconCls : 'remove',
                        scope: this,
                        handler: this.del
                    }
                ]
            },
            bbar: {
                xtype: 'paging',
                pageSize: 15,
                displayInfo: true,
                store: this.ds
            }
        });
        this.main.add(this.body);
        this.main.doLayout();
        this.ds.load({
            params: {start:0, limit:15 }
        });
        this.render();
    },
    getFruittypeDialog: function(){
        var dlg = new Ext.Window({
            height: 200,
            width: 400,
            layout: 'fit',
            border: false,
            frame: true,
            title: '',
            closeAction: 'hide',
            modal: true,
            plain: true,
            resizable: false,
            items: [this.form],
            listeners: {
                scope: this,
                render: function(fp){
                    this.form.getForm().waitMsgTarget = fp.getEl();
                },
                show: function(){
                    var f = this.form.getForm();
                    f.setValues(this.currentFormValues);
                    f.clearInvalid();
                }
            }
        });
        return dlg;
    },
    getForm: function() {
        var form = new Ext.form.FormPanel({
            defaultType: 'textfield',
            bodyStyle: 'padding:5px',
            buttonAlign: 'center',
            url:'fruittype/save.do',
            labelWidth: 80,
            submitEmptyText: false,     //禁止提交emptyText
            defaults: {
                anchor: '90%'
            },
            items: [
                {
                    xtype: 'hidden',
                    name: 'id',
                    fieldLabel: 'Label'
                },
                {
                    xtype: 'textfield',
                    name: 'name',
                    allowBlank: false,
                    emptyText: '成果类别名称不能为空',
                    fieldLabel: '成果类别名称'
                },
                {
                    xtype: 'textarea',
                    name: 'description',
                    fieldLabel: '描述'
                }
            ],
            buttons: [{
                text: '确定',
                scope: this,
                handler: function() {
                    this.submit();
                }
            },
                {
                    text: '重置',
                    scope: this,
                    handler: function() {
                        var f = this.form.getForm();
                        f.reset();
                        f.setValues(this.currentFormValues);
                        f.clearInvalid();
                    }
                }]
        });
        return form;
    },
    submit: function(){
        if (this.form.getForm().isValid()) {
            var id = this.form.getForm().findField("id").getValue();
            this.form.getForm().submit({
                waitTitle: '保存数据',
                waitMsg: '正在保存……',
                scope: this,
                method: 'post',
                params: '',
                success: function(form, action) {
                    var info = action.result.info;
                    if (action.result.success) {
                        this.ds.reload();
                        if (action.result.method == "Create") {
                            this.form.getForm().reset();
                        } else {
                            this.dlg.hide();
                        }
                    }
                    Ext.Msg.alert('信息', info);
                },
                failure: function(form, action) {
                    var info = action.result.info;
                    Ext.Msg.alert('提示', info);
                }
            });
        }
    },
    add: function() {
        Ext.apply(this.currentFormValues, {
            id: '',
            name: "",
            description: ""
        });
        this.dlg.setTitle("增加成果类别");
        this.dlg.show();
    },

    edit: function() {
        var recs = this.body.getSelectionModel().getSelections();
        if (recs.length==0) {
            Ext.Msg.alert('信息', '请选择要编辑的成果类别。');
        } else if(recs.length>1) {
            Ext.Msg.alert('信息', '只能选择一个成果类别进行编辑。');
        } else {
            Ext.apply(this.currentFormValues, {
                id: recs[0].data.id,
                name: recs[0].data.name,
                description: recs[0].data.description
            });
            this.dlg.setTitle("编辑成果类别");
            this.dlg.show();
        }
    },

    del: function() {
        mainPanel = this;
        if (this.body.getSelectionModel().hasSelection()) {
            var recs = this.body.getSelectionModel().getSelections();
            var ids = [];
            var titles = [];
            for (var i = 0; i < recs.length; i++) {
                var data = recs[i].data;
                ids.push(data.id);
                titles.push(data.name);
            }
            Ext.Msg.confirm('删除成果类别', '确定删除以下成果类别？<br><font color="red">' + titles.toString() + '</font>',
                function(btn) {
                    if (btn == 'yes') {
                        Ext.Ajax.request({
                            method:'post',
                            url:'fruittype/remove.do',
                            params:{
                                ids:ids.toString()
                            },
                            scope: mainPanel,
                            success: function(resp, opts) {
                                var result = Ext.util.JSON.decode(resp.responseText);
                                var info = result.info;
                                if(result.success=='true') {
                                    this.ds.reload();
                                    Ext.Msg.alert('信息', info);
                                }else {
                                    Ext.Msg.alert('信息', info);
                                }
                            },
                            failure: function(resp,opts) {
                                var result = Ext.util.JSON.decode(resp.responseText);
                                var info = result.info;
                                Ext.Msg.alert('提示', info);
                            }
                        });
                    }
                });
        } else {
            Ext.Msg.alert('信息', '请选择要删除的成果类别！');
        }
    },
    render: function() {
        if (!this.form) {
            this.form = this.getForm();
        };
        if (!this.dlg) {
            this.dlg = this.getFruittypeDialog();
        };
    }
});
